package com.qiantu.phone.widget.viewmodel;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.ElecPeriodBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.app.AppFragment;
import com.qiantu.phone.ui.activity.EnergyStatisticsActivity;

public class EnergyConsumptionPresenter implements LifecycleEventObserver , View.OnClickListener {
    private AppFragment mParent;
    private TextView tvTodayValue;
    private TextView tvMonthValue;
    public EnergyConsumptionPresenter(AppFragment parent, View itemView) {
        mParent = parent;
        tvTodayValue = itemView.findViewById(R.id.today_value);
        tvMonthValue = itemView.findViewById(R.id.month_value);
        itemView.setOnClickListener(this::onClick);
        mParent.getLifecycle().addObserver(this);
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
        switch (event){
            case ON_RESUME:{
                //getEnergyData();
            }
        }
    }

    public void getEnergyData() {
        LLHttpManager.getElecStatistics(mParent, true, AppApplication.getInstance().getCurrentHouseSerialNo(), null, new HttpCallback<HttpData<ElecPeriodBean>>(null){
            @Override
            public void onSucceed(HttpData<ElecPeriodBean> result) {
                super.onSucceed(result);
                ElecPeriodBean data = result.getData();
                tvTodayValue.setText(String.format("%.2f", data.getToday().elec));
                tvMonthValue.setText(String.format("%.2f", data.getLastThirtyDays().elec));
            }
        });
    }

    @Override
    public void onClick(View v) {
        mParent.startActivity(EnergyStatisticsActivity.class);
    }

    interface EnergyConsumption{

    }
}
